package com.sskj.b70.utils;

import android.text.TextPaint;
import android.util.TypedValue;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.b70.common.App;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-08-16 17:13
 */
public class TextSizeUtil {
    public static void adjustTvTextSize(TextView tv, int maxWidth, CharSequence text) {
        int avaiWidth = maxWidth - tv.getPaddingLeft() - tv.getPaddingRight() - 10;

        if (avaiWidth <= 0) {
            return;
        }

        TextPaint textPaintClone = new TextPaint(tv.getPaint());
        // note that Paint text size works in px not sp
//        float trySize = textPaintClone.getTextSize();
        float trySize = RudenessScreenHelper.pt2px(App.INSTANCE,30);
        textPaintClone.setTextSize(trySize);
        while (textPaintClone.measureText(text.toString()) > avaiWidth) {
            trySize--;
            textPaintClone.setTextSize(trySize);
        }

        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
    }

}
